﻿<?php

include("class.base.php");
if ($sess = sisse_logitud()) {
	if (!$minu_grupi_oigused = grupi_oigused($sess)) {
		$minu_grupi_oigused = 0;
	}
	$minuLemmikud = mysql_query("SELECT * FROM lemmikud WHERE lisaja=".$sess["id"]);
	$lemmikud = array();
	while ($lemmik = mysql_fetch_array($minuLemmikud)) {
		array_push($lemmikud, $lemmik["tabel"]."-".$lemmik["vali"]);
	}
	$jsLemmik = "";
}
else {
	$minu_grupi_oigused = 0;
	if (isset($_SESSION["lemmikud"])) { $lemmikud = $_SESSION["lemmikud"]; }
	else { $lemmikud = array(); }
	$jsLemmik = "_a";
}

if ($minu_grupi_oigused <= 0) { //sisse logimata
	$tabelid = getPublicTables();
	$excludes = excludes();
}
elseif ($minu_grupi_oigused <= 1) { //otsimine
	$tabelid = getNonSystemTables();
	$excludes = excludes();
}
elseif ($sess["grupp"] <= 2) { //otsimine, lisamine
	$tabelid = getNonSystemTables();
	$excludes = excludes(0);
}
else { //admin
	$tabelid = get_tables();
	$excludes = array();
}

$allTables = get_tables();
$nonSystemTables = getNonSystemTables();

$tabel1 = kriips($_GET["t"]);
$xtabel = htmlspecialchars($_GET["t"], ENT_QUOTES);
$t_id = htmlspecialchars($_GET["id"], ENT_QUOTES);

$orig_aeg = array(
"audio_trakk" => "salvestusaeg",
"video_trakk" => "salvestusaeg",
"foto" => "pildistamisaeg",
"kasikiri" => "koostamise_aeg");

$pealkiri1 = $tabel1;
$sisu =<<<SISU
	<table class="colspace">
SISU;

if (isset($_GET["muuda"])) {
	if ($_GET["muuda"]) { $teade = $lang["kirje_muutmine_onnestus1"]; }
	else { $viga = $lang["kirje_muutmine_onnestus0"]; }
}

if (in_array($xtabel, $tabelid)) {
	if ($minu_grupi_oigused <= 0) { $where = " AND oigused='avalik'"; }
	else { $where = ""; }
	$paring = mysql_query("SELECT * FROM ".$xtabel." WHERE id='".$t_id."'".$where);
	$roles_columns = get_roles_columns($xtabel);
	if (mysql_num_rows($paring)) {
		$r = mysql_fetch_array($paring);
		$resource_name = $xtabel."-".$t_id;
		$sisu .= "<tr><td style='min-width: 175px; max-width: 175px;'>";
		if (isset($_SESSION["otsing"])) {
			$sisu .= "<a href='".$_SESSION["otsing"]."'>".$lang["viimati_sooritatud_otsing"]."</a>";
		}
		$sisu .= "</td><td style='width: 100%'></td></tr>";
		$sisu2 = "";
		if (isset($_FILES['userfile']) && $minu_grupi_oigused == 3) {
			if ($xtabel == "audio_trakk") {
				$allowed_filetypes = array('wav');
			}
			elseif ($xtabel == "foto") {
				$allowed_filetypes = array('jpg');
			}
			elseif ($xtabel == "kasikiri") {
				$allowed_filetypes = array('jpg','pdf');
			}
			else {
				$allowed_filetypes = array();
			}
			$uploaddir = str_replace(array("..", "AUDIO_PATH/", "PHOTO_PATH/", "KASIKIRI_PATH/"), array("", AUDIO_PATH, PHOTO_PATH, KASIKIRI_PATH), $_POST["uploadDir"]);
			$filename = $_FILES['userfile']['name'];
			if (!strstr($filename, $r["arhiivinumber"])) {
				$filename = $r["arhiivinumber"]."_".$filename;
			}
			$ext2 = array_reverse(explode('.', $filename));
			$ext = strtolower($ext2[0]);
			if (!is_dir($uploaddir)) { mkdir($uploaddir, 0777, true) or $viga = "Üleslaadimiseks pole sobilikku kataloogi olemas ja seda ei saanud luua"; }
			if (!is_writable($uploaddir)) { chmod($uploaddir, 0777) or $viga = "Tekkis viga üleslaadimise asukoha failiõigustega"; }
			if (!in_array($ext,$allowed_filetypes)) {
				$viga = "Lubatud faililaiendid: ".implode(', ', $allowed_filetypes)." (proovisid ".$ext.")";
			}
			elseif (file_exists($uploaddir.'/'.$filename)) {
				$viga = $filename." on juba olemas";
			}
			elseif (is_writable($uploaddir)) {
				move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.'/'.$filename);
				if (file_exists($uploaddir.'/'.$filename)) {
					$teade = "Üles laadimine on edukalt lõpetatud";
				}
				else {
					$viga = "Üles laadimisel tekkis tundmatu viga";
				}
			}
		}
		if ($xtabel == "audio_trakk") {
			$sari = mysql_fetch_array(mysql_query("SELECT * FROM audio WHERE id=".$r["audio"]));
			$sarjaParing = mysql_fetch_array(mysql_query("SELECT * FROM sari WHERE id=".$sari["sari"]));
			$eemaldaSari = explode($sarjaParing["sarja_tahis"], $r["arhiivinumber"]);
			$filePath = AUDIO_PATH.$sarjaParing["sarja_tahis"]."_WAV/".$sarjaParing["sarja_tahis"].substr($eemaldaSari[1], 0, 2);
			if (is_dir($filePath)) {
				$handler = opendir($filePath); //otsi üles audiofailid
				$piltide_nimekiri = array();
				while ($file = readdir($handler)) {
					if ($file != "." and $file != "..") {
						if (stristr($file, $r["arhiivinumber"]) and stristr($file, "wav")) {
							$finalWAVFile = $filePath."/".$file;
							break;
						}
					}
				}
				closedir($handler);
				if ($finalWAVFile) {
					$checkMP3 = array_reverse(explode("/", $finalWAVFile));
					$finalMP3File = "mp3_audio/".str_ireplace("wav", "mp3", $checkMP3[0]);
					if (isset($_POST["kustutamp3"]) && $minu_grupi_oigused == 3 && file_exists($finalMP3File)) {
						unlink($finalMP3File);
					}
					if (file_exists($finalMP3File) && filemtime($finalWAVFile) < filemtime($finalMP3File)) {
						$sisu2 .= "<tr><td><object type=\"application/x-shockwave-flash\" data=\"audio/dewplayer.swf?mp3=".$finalMP3File."\" width=\"200\" height=\"20\" id=\"dewplayer\"><param name=\"wmode\" value=\"transparent\" /><param name=\"movie\" value=\"audio/dewplayer.swf?mp3=".$finalMP3File."\" /></object>";
						if ($minu_grupi_oigused == 3) {
							$kustutamp3 = " <form action='' method='POST' style='display: inline;'><input type='submit' name='kustutamp3' value='".$lang["kustuta"]." MP3' /></form>";
						}
						$sisu2 .= "</td></tr>";
					}
					else {
						$sisu2 .= "<tr><td><span id=\"kuula\"><a href=\"#\" onclick=\"kuula('".str_replace(AUDIO_PATH, "AUDIO_PATH/", $finalWAVFile)."'); return false;\">".$lang["kuula"]."</a></span>";
						$sisu2 .= "<span id=\"kuulamp3\" style=\"display: none;\"><object type=\"application/x-shockwave-flash\" data=\"audio/dewplayer.swf?mp3=".$finalMP3File."\" width=\"200\" height=\"20\" id=\"dewplayer\"><param name=\"wmode\" value=\"transparent\" /><param name=\"movie\" value=\"audio/dewplayer.swf?mp3=".$finalMP3File."\" /></object></span>";
						$sisu2 .= "<span id=\"laen\" style=\"display: none;\"><img src=\"kujundus/pildid/loading.gif\" alt=\"".$lang["laen"]."\" title=\"".$lang["laen"]."\" /></span></td></tr>";
					}
					if ($minu_grupi_oigused > 0) {
						$sisu2 .= "<tr><td><a href='get_wav.php?path=".str_replace(AUDIO_PATH, "AUDIO_PATH/", $finalWAVFile)."'>".$lang["lae_alla"]."</a></td></tr>";
					}
				}
			}
			if (!$sisu2) { $uploadDir = str_replace(AUDIO_PATH, "AUDIO_PATH/", $filePath); }
		}
		elseif ($xtabel == "foto") {
			$album = mysql_fetch_array(mysql_query("SELECT * FROM fotoalbum WHERE id=".$r["fotoalbum"]));
			$sarjaParing = mysql_fetch_array(mysql_query("SELECT * FROM sari WHERE id=".$album["sari"]));
			$filedir = PHOTO_PATH.$sarjaParing["sarja_tahis"]."/".$album["arhiivinumber"];
			if (is_dir($filedir)) {
				$handler = opendir($filedir); //otsi üles kõik albumi pildid
				while ($file = readdir($handler)) {
					if ($file != "." and $file != "..") {
						$isitimage = array_reverse(explode(".", $file));
						if ($isitimage[0] == "JPG" or $isitimage[0] == "jpg") {
							if (stristr($file, $r["arhiivinumber"])) {
								$sisu2 .= "<tr><td><a href='naita_pilt.php?materjal=foto&materjal_id=".$r["arhiivinumber"]."&sari=".$sarjaParing["sarja_tahis"]."&album=".$album["arhiivinumber"]."&formaat=PHOTO' target='_blank'>".$lang["vaata_fotot"]."</a></td><td></td><td rowspan='20'><a href='naita_pilt.php?materjal=foto&materjal_id=".$r["arhiivinumber"]."&sari=".$sarjaParing["sarja_tahis"]."&album=".$album["arhiivinumber"]."&formaat=PHOTO' target='_blank'><img src='naita_pilt.php?materjal=foto&materjal_id=".$r["arhiivinumber"]."&sari=".$sarjaParing["sarja_tahis"]."&album=".$album["arhiivinumber"]."&formaat=PHOTO&pisi=1' style='width: auto; height: auto; max-width: 300px; max-height: 300px;' /></a></td></tr>";
								break;
							}
						}
					}
				}
				closedir($handler);
			}
			if (!$sisu2) { $uploadDir = str_replace(PHOTO_PATH, "PHOTO_PATH/", $filedir); }
		}
		elseif ($xtabel == "kasikiri") {
			$sarjaParing = mysql_fetch_array(mysql_query("SELECT * FROM sari WHERE id=".$r["sari"]));
			$fileDir = KASIKIRI_PATH.$sarjaParing["sarja_tahis"]."/".$r["arhiivinumber"];
			if (is_dir($fileDir)) {
				$sisu2 .= "<tr><td><a href='naita_pilt.php?materjal=kasikiri&materjal_id=".$r["arhiivinumber"]."&sari=".$sarjaParing["sarja_tahis"]."&formaat=".$r["originaali_formaat"]."' target='_blank'>".$lang["vaata_kasikirju"]."</a></td></tr>";
				if ($sess) {
					$sisu2 .= "<tr><td><a href='get_zip.php?path=".str_replace(KASIKIRI_PATH, "KASIKIRI_PATH/", $fileDir)."'>".$lang["lae_kasikirjad_alla"]."</a></td></tr>";
				}
			}
			$uploadDir = str_replace(KASIKIRI_PATH, "KASIKIRI_PATH/", $fileDir);
		}
		$sisu .= "<tr><td colspan='2'>";
		if ($minu_grupi_oigused > 2 or $r["lisaja"] == $sess["id"]) {
			$sisu .= "
				<a href='muuda.php?t=".$xtabel."&id=".$t_id."'><img src='kujundus/pildid/muuda.png' title='".$lang["muuda"]."' alt='".$lang["muuda"]."' /></a> 
				<a href='kustuta.php?t=".$xtabel."&id=".$t_id."'><img src='kujundus/pildid/kustuta.png' title='".$lang["kustuta"]."' alt='".$lang["kustuta"]."' /></a>
			";
		}
		$sisu .= "
			<span id='".$resource_name."'>
		";
		if (in_array($resource_name, $lemmikud)) {
			$sisu .= "<img src='kujundus/pildid/ok.png' alt='".$lang["sinu_lemmik"]."' title='".$lang["sinu_lemmik"]."'>";
		}
		else {
			$sisu .= "<img src='kujundus/pildid/lemmik.png' title='".$lang["lisa_lemmikutesse"]."' alt='".$lang["lisa_lemmikutesse"]."' onclick=\"lisa_lemmik".$jsLemmik."('".$xtabel."', '".$t_id."'); this.style.display='none'; \" />";
		}
		$sisu .= "</span>";
		if (isset($kustutamp3)) { $sisu .= $kustutamp3; }
		if ($minu_grupi_oigused == 3 && isset($uploadDir)) { $sisu .= " <form action='' method='POST' enctype='multipart/form-data' style='display: inline;'><input type='hidden' name='uploadDir' value='".$uploadDir."' /><input type='file' name='userfile' /><input type='submit' value='Lae üles' /></form>"; }
		$sisu .= "</td></tr>";
		$sisu .= $sisu2;
		if (isset($r["arhiivinumber"])) {
			if ($minu_grupi_oigused <= 0) {
				$searchWhere = " WHERE oigused='avalik'";
			}
			else {
				$searchWhere = "";
			}
			$sql = "SELECT * FROM ".$xtabel.$searchWhere." ORDER BY arhiivinumber ASC";
			if ($jarjekorraParing = mysql_query($sql)) {
				while ($jarjekorraRida = mysql_fetch_array($jarjekorraParing)) {
					if ($jarjekorraRida["arhiivinumber"] == $r["arhiivinumber"]) {
						if (isset($eelmineRida)) {
							$naitaEelmine = "<a href='naita.php?t=".$xtabel."&id=".$eelmineRida["id"]."'> &laquo; ".$eelmineRida["arhiivinumber"]."</a>";
							unset($eelmineRida);
						}
						$jargmineRida = 1;
					}
					elseif (isset($jargmineRida)) {
						$naitaJargmine = "<a href='naita.php?t=".$xtabel."&id=".$jarjekorraRida["id"]."'>".$jarjekorraRida["arhiivinumber"]." &raquo; </a>";
						break;
					}
					else {
						$eelmineRida = $jarjekorraRida;
					}
				}
			}
			if (isset($naitaEelmine) or isset($naitaJargmine)) {
				$sisu .= "<tr><td colspan='2'>".$naitaEelmine." ".$naitaJargmine."</td><tr>";
			}
		}
		$result = mysql_query("SHOW COLUMNS FROM ".$xtabel);
		while ($columns = mysql_fetch_array($result)) {
			if (!in_array($columns[0], $excludes)) {
				$n = trim($r[$columns[0]]); //eemalda whitespace'd ja muuda ära erisümbolid
				$tyyp = $columns[1]; //otsitava andme tüüp
				$tyyp = explode("(", $tyyp);
				$tyyp = $tyyp[0]; //int, varchar, date, datetime etc
				if ($columns[0] == "keel" && $xtabel != "materjal_keel") {
					$keeled = array();
					$query = mysql_query("SELECT id,keel,lisaja FROM materjal_keel WHERE materjal='".$xtabel."' AND materjal_id='".$r["id"]."'");
					while ($row = mysql_fetch_array($query)) {
						if ($sess && $sess["id"] == $row["lisaja"] || $minu_grupi_oigused > 2) {
							$row["keel"] = "<a href='naita.php?t=materjal_keel&id=".$row["id"]."'>".$row["keel"]."</a>";
						}
						$keeled[] = $row["keel"];
					}
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>".implode(", ", $keeled);
					if (in_array("materjal_keel", $tabelid) and $minu_grupi_oigused > 1) {
						$sisu .= "<br /><i><a href='lisa.php?t=materjal_keel&u=".$xtabel."&id=".$t_id."&ref=naita&typ=alam'>".$lang["lisa"]."</a></i>";
					}
					$sisu .= "</td></tr>";
				}
				elseif (in_array($columns[0], $allTables)) {
					$paring2 = mysql_query("SELECT * FROM ".$columns[0]." WHERE id='".$n."'");
					if ($paring2) {
						$rida2 = mysql_fetch_array($paring2);
						$must_show_columns = get_must_show_wide_columns($columns[0]);
						$sisu .= "
							<tr><td>".kriips($columns[0])."</td><td>";
						if (in_array($columns[0], $tabelid)) {
							$sisu .= "<a href=\"naita.php?t=".$columns[0]."&id=".$n."\">";
						}
						foreach ($must_show_columns as $value) {
							$sisu .= " ".$rida2[$value];
						}
						if (in_array($columns[0], $tabelid)) {
							$sisu .= "</a>";
						}
						$sisu .= "</td></tr>";
						$paring3 = mysql_query("SHOW COLUMNS FROM ".$columns[0]);
						while ($rida3 = mysql_fetch_array($paring3)) {
							if (in_array($rida3[0], $allTables)) {
								if ($paring4 = mysql_query("SELECT * FROM ".$rida3[0]." WHERE id=".$rida2[$rida3[0]])) {
									$rida4 = mysql_fetch_array($paring4);
									$must_show_columns = get_must_show_wide_columns($rida3[0]);
									$sisu .= "
										<tr><td>".kriips($rida3[0])."</td><td>";
									if (in_array($rida3[0], $tabelid)) {
										$sisu .= "<a href=\"naita.php?t=".$rida3[0]."&id=".$rida2[$rida3[0]]."\">";
									}
									foreach ($must_show_columns as $value) {
										$sisu .= " ".$rida4[$value];
									}
									if (in_array($rida3[0], $tabelid)) {
										$sisu .= "</a>";
									}
									$sisu .= "</td></tr>";
								}
							}
						}
					}
					else {
						$sisu .= "
							<tr><td>".kriips($columns[0])."</td><td>".$n."</td></tr>";
					}
				}
				elseif ($tyyp == "text") {
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>".nl2br($n)."</td></tr>";
				}
				elseif ($tyyp == "date") {
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>".date_to_est($n)."</td></tr>";
				}
				elseif ($columns[0] == "ressursi_tyyp") {
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>";
					if ($n == '1') { $sisu .= $lang["fyysiline"]; }
					elseif ($n == '2') { $sisu .= $lang["digitaalne"]; }
					$sisu .= "</td></tr>";
				}
				elseif ($columns[0] == "fyysiliselt_olemas") {
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>";
					if ($n == '1') { $sisu .= $lang["olemas"]; }
					elseif ($n == '2') { $sisu .= $lang["kadunud"]; }
					elseif ($n == '2') { $sisu .= $lang["laenatud"]; }
					$sisu .= "</td></tr>";
				}
				elseif ($columns[0] == "digitaalselt_olemas") {
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>";
					if ($n == '1') { $sisu .= $lang["jah"]; }
					elseif ($n == '2') { $sisu .= $lang["ei"]; }
					$sisu .= "</td></tr>";
				}
				elseif ($columns[0] == "kestus") {
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>".pretty_length($n)."</td></tr>";
				}
				elseif ($columns[0] == "lisaja") {
					if ($query = mysql_query("SELECT email FROM kasutaja WHERE id=".$n)) {
						$row = mysql_fetch_array($query);
						$sisu .= "
							<tr><td>Lisaja</td><td><a href=\"naita.php?t=kasutaja&id=".$n."\">".$row["email"]."</a></td></tr>";
					}
					else {
						$sisu .= "
							<tr><td>".kriips($columns[0])."</td><td>".$n."</td></tr>";
					}
				}
				elseif ($columns[0] == "materjal") {
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>".kriips($n)."</td></tr>";
				}
				elseif ($columns[0] == "materjal_id" and $paring2 = mysql_query("SELECT * FROM ".$r["materjal"]." WHERE id=".$n)) {
					$rida2 = mysql_fetch_array($paring2);
					$must_show_columns = get_must_show_wide_columns($r["materjal"]);
					$sisu .= "
						<tr><td></td><td><a href='naita.php?t=".$r["materjal"]."&id=".$n."'>";
					foreach ($must_show_columns as $value) {
						if ($value == "kestus") {
							$sisu .= " ".pretty_length($rida2[$value]);
						}
						else {
							$sisu .= " ".$rida2[$value];
						}
					}
					$sisu .= "</a></td></tr>";
				}
				elseif ($columns[0] == "seostuvad_viited" && $n) {
					$arhNrid = explode(",", $n);
					$n = "";
					foreach ($arhNrid as $arhNr) {
						$arhNr1 = explode("-",substr($arhNr, 0, -1));
						$sarjaTahis = trim(str_replace(range(0,9),"",$arhNr1[0]));
						$materjal = mysql_fetch_array(mysql_query("SELECT materjali_tyyp FROM sari WHERE sarja_tahis='".$sarjaTahis."'"));
						if ($materjal && table_exists($materjal["materjali_tyyp"])) {
							$viite_id = mysql_fetch_array(mysql_query("SELECT id FROM ".$materjal["materjali_tyyp"]." WHERE arhiivinumber='".trim($arhNr)."'"));
							if (!$viite_id) {
								$materjali_tyyp = str_replace("_trakk", "", $materjal["materjali_tyyp"]);
								$viite_id = mysql_fetch_array(mysql_query("SELECT id FROM ".$materjali_tyyp." WHERE arhiivinumber='".trim($arhNr)."'"));
							}
							if (!$viite_id) {
								$materjali_tyyp = $materjal["materjali_tyyp"]."album";
								if ($viite_id = mysql_query("SELECT id FROM ".$materjali_tyyp." WHERE arhiivinumber='".trim($arhNr)."'")) {
									$viite_id = mysql_fetch_array($viite_id);
								}
							}
							if ($viite_id["id"]) {
								$n .= "<a href='naita.php?t=".$materjal["materjali_tyyp"]."&id=".$viite_id["id"]."'>".trim($arhNr)."</a>, ";
							}
							else { $n .= trim($arhNr).", "; }
						}
						else { $n .= trim($arhNr).", "; }
					}
					$n = rtrim($n, ", ");
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>".$n."</td></tr>";
				}
				else {
					$sisu .= "
						<tr><td>".kriips($columns[0])."</td><td>".$n."</td></tr>";
				}
				
				if ($roles_columns[0] == $columns[0]) { //digitaliseerijad
					$sisu .= "
						<tr><td>".$lang["digitaliseerijad"]."</td><td>";
					$query = mysql_query("SELECT * FROM digitaliseerija WHERE materjal='".$xtabel."' AND materjal_id='".$t_id."'");
					if (mysql_num_rows($query)) {
						$must_show_columns = get_must_show_wide_columns("digitaliseerija");
						$must_show_columns2 = get_must_show_wide_columns("osaleja");
						while ($row = mysql_fetch_array($query)) {
							if ($query2 = mysql_query("SELECT * FROM osaleja WHERE id=".$row["osaleja"])) {
								$row2 = mysql_fetch_array($query2);
								foreach ($must_show_columns as $value) {
									if ($value == "osaleja") {
										if (in_array("osaleja", $tabelid)) {
											$sisu .= " <a href='naita.php?t=osaleja&id=".$row["osaleja"]."'>";
										}
										foreach ($must_show_columns2 as $value2) {
											$sisu .= " ".$row2[$value2];
										}
										if (in_array("osaleja", $tabelid)) {
											$sisu = rtrim($sisu, " ");
											$sisu .= "</a>";
										}
									}
									elseif ($value == "vanus" && ($row["vanus"] || $orig_aeg[$xtabel] && $row["vanus"] = vanus($row2["synniaasta"], $r[$orig_aeg[$xtabel]]))) {
										$sisu .= " (".$row["vanus"].")";
									}
									elseif ($value == "materjal") {}
									else {
										$sisu .= " ".$row[$value];
									}
								}
								$sisu .= "<br />";
							}
						}
					}
					if (in_array("digitaliseerija", $tabelid) and $minu_grupi_oigused > 1) {
						$sisu .= "<i><a href='lisa.php?t=digitaliseerija&u=".$xtabel."&id=".$t_id."&ref=naita&typ=alam'>".$lang["lisa"]."</a></i>";
					}
					$sisu .= "</td></tr>";
				}
				if ($roles_columns[1] == $columns[0]) { //rollid
					$order_by = "ORDER BY FIELD(rolli_nimetus";
					$rolli_nimetus = mysql_query("SELECT id FROM rolli_nimetus ORDER BY jrknr DESC");
					while ($rolli_nimi = mysql_fetch_array($rolli_nimetus)) {
						$order_by .= ",'".$rolli_nimi["id"]."'";
					}
					$order_by .= ") DESC";
					$sisu .= "
						<tr><td>".$lang["muud_rollid"]."</td><td>";
					$query = mysql_query("SELECT * FROM roll WHERE materjal='".$xtabel."' AND materjal_id='".$t_id."' ".$order_by);
					if (mysql_num_rows($query)) {
						$must_show_columns = get_must_show_wide_columns("roll");
						$must_show_columns2 = get_must_show_wide_columns("osaleja");
						while ($row = mysql_fetch_array($query)) {
							if ($query2 = mysql_query("SELECT * FROM osaleja WHERE id=".$row["osaleja"])) {
								$row2 = mysql_fetch_array($query2);
								foreach ($must_show_columns as $value) {
									if ($value == "osaleja") {
										if (in_array("osaleja", $tabelid)) {
											$sisu .= "<a href='naita.php?t=osaleja&id=".$row["osaleja"]."'>";
										}
										$tykid = explode("{", OSALEJA_FORMAAT);
										foreach ($tykid as $count => $tykk) {
											if ($count == 0) {
												$sisu .= $tykk;
											}
											else {
												$tykid2 = explode("}", $tykk);
												$tykk2 = $tykid2[0];
												if ($row2[$tykk2]) {
													$sisu .= $row2[$tykk2];
													if ($count == count($tykid)-1) {
														$sisu .= $tykid2[1];
													}
													else {
														for ($i = 1; isset($tykid[$count+$i]); $i++) {
															$tykid3 = explode("}", $tykid[$count+$i]);
															if ($row2[$tykid3[0]]) {
																$sisu .= $tykid2[1];
																break;
															}
														}
													}
												}
											}
										}
										if (in_array("osaleja", $tabelid)) {
											$sisu .= "</a>";
										}
									}
									elseif ($value == "vanus" && ($row["vanus"] || $orig_aeg[$xtabel] && $row["vanus"] = vanus($row2["synniaasta"], $r[$orig_aeg[$xtabel]]))) {
										$sisu .= " (".$row["vanus"].")";
									}
									elseif ($value == "materjal") {}
									elseif ($value == "rolli_nimetus" && in_array("roll", $tabelid)) {
										$sisu .= " <a href='naita.php?t=roll&id=".$row["id"]."'>".$row["rolli_nimetus"]."</a> ";
									}
									else {
										$sisu .= " ".$row[$value]." ";
									}
								}
								$sisu .= "<br />";
							}
						}
					}
					if (in_array("roll", $tabelid) and $minu_grupi_oigused > 1) {
						$sisu .= "<i><a href='lisa.php?t=roll&u=".$xtabel."&id=".$t_id."&ref=naita&typ=alam'>".$lang["lisa"]."</a></i>";
					}
					$sisu .= "</td></tr>";
				}
			}
		}
		foreach ($allTables as $tabel) {
			$alamad = mysql_query("SHOW COLUMNS FROM ".$tabel);
			while ($rida = mysql_fetch_array($alamad)) {
				if ($rida[0] == "lisaja") { $rida[0] = "kasutaja"; }
				if ($rida[0] == $xtabel) { //leiti alamtabel
					if ($rida[0] == "kasutaja") { $rida[0] = "lisaja"; }
					$paring = mysql_query("SELECT * FROM ".$tabel." WHERE ".$rida[0]."='".$t_id."'");
					if (mysql_num_rows($paring)) {
						$must_show_columns = get_must_show_wide_columns($tabel);
						$sisu .= "
							<tr><td>".kriips($tabel)."</td><td>";
						while ($rida = mysql_fetch_array($paring)) {
							if (in_array($tabel, $tabelid)) {
								$sisu .= "<a href='naita.php?t=".$tabel."&id=".$rida["id"]."'>";
							}
							if (in_array("vanus", $must_show_columns)) {
								$row2 = mysql_query("SELECT * FROM ".$rida["materjal"]." WHERE id=".$rida["materjal_id"]);
								if ($row2 && $row2 = mysql_fetch_array($row2)) {
									if (isset($row2[$orig_aeg[$rida["materjal"]]])) { $orig_salvestus_aeg = $row2[$orig_aeg[$rida["materjal"]]]; }
								}
							}
							foreach ($must_show_columns as $value) {
								if ($value == "kestus") {
									$sisu .= " ".pretty_length($rida[$value]);
								}
								elseif ($value == "materjal") {
									if ($query = mysql_query("SELECT * FROM ".$rida["materjal"]." WHERE id=".$rida["materjal_id"])) {
										$sisu = rtrim($sisu, " ");
										$sisu .= ": ".kriips($rida["materjal"]);
										$row = mysql_fetch_array($query);
										$must_show_columns2 = get_must_show_wide_columns($rida["materjal"]);
										foreach ($must_show_columns2 as $value2) {
											if ($value2 == "kestus") {
												$sisu .= " ".pretty_length($row[$value2]);
											}
											else {
												$sisu .= " ".$row[$value2];
											}
										}
									}
								}
								elseif (in_array($value, $nonSystemTables)) {
									if ($query = mysql_query("SELECT * FROM ".$value." WHERE id=".$rida[$value])) {
										$row = mysql_fetch_array($query);
										$must_show_columns2 = get_must_show_wide_columns($value);
										foreach ($must_show_columns2 as $value2) {
											if ($value2 == "kestus") {
												$sisu .= " ".pretty_length($row[$value2]);
											}
											else {
												$sisu .= " ".$row[$value2];
											}
										}
									}
								}
								elseif ($value == "vanus" && ($rida["vanus"] || $rida["vanus"] = vanus($r["synniaasta"], $orig_salvestus_aeg))) {
									$sisu .= " (".$rida[$value].")";
								}
								else {
									$sisu .= " ".$rida[$value];
								}
							}
							if (in_array($tabel, $tabelid)) {
								$sisu .= "</a>";
							}
							$sisu .= "<br />";
							$orig_salvestus_aeg = "";
						}
						if ($xtabel == "osaleja" and $minu_grupi_oigused > 1) {
							$sisu .= "<i><a href='lisa.php?t=".$tabel."&u=".$xtabel."&id=".$t_id."&ref=naita'>".$lang["lisa"]."</a></i>";
						}
						elseif ($minu_grupi_oigused > 1) {
							$sisu .= "<i><a href='lisa.php?t=".$tabel."&u=".$xtabel."&id=".$t_id."&ref=naita&typ=alam'>".$lang["lisa"]."</a></i>";
						}
						$sisu .= "</td></tr>";
					}
				}
			}
		}
	} else { $viga = $lang["sellist_kirjet_ei_leitud"]; }
} else { $viga = $lang["tabelit_ei_leitud"]; }
$sisu .=<<<SISU
	</table>
SISU;
include("kujundus.php");
?>
